package com.naton.common.network

import android.util.Log
import okhttp3.FormBody
import okhttp3.Interceptor
import okhttp3.MultipartBody
import okhttp3.Response
import java.net.URLDecoder

/**
 * 公共参数拦截器
 */
abstract class AddParameterInterceptor : Interceptor {


    abstract fun getPubParamsMap() : HashMap<String , String>

    override fun intercept(chain: Interceptor.Chain): Response {
        val pubParamsMap = getPubParamsMap()
        var oRequest = chain.request()
        var paramsString = ""
        if ("POST" == oRequest.method){
            var oldBody = oRequest.body
            val bodyBuilder = FormBody.Builder()
            when(oldBody){
                is MultipartBody ->{
                    //文件上传
                    val mulBuilder = MultipartBody.Builder()
                    val oldPartList = oldBody.parts
                    mulBuilder.setType(MultipartBody.FORM)
                    for (part in oldPartList) {
                        mulBuilder.addPart(part)
                    }
                    for ((key , value) in pubParamsMap){
                        paramsString = "$paramsString$key:$value"
                        mulBuilder.addFormDataPart(key , value)
                    }
                    oldBody = mulBuilder.build()
                }
                is FormBody -> {  //普通的Form表单
                    //把原来的参数添加到新的构造器，（因为没找到直接添加，所以就new新的）
                    for (i in 0 until oldBody.size) {
                        val name = oldBody.encodedName(i)
                        val value = oldBody.encodedValue(i)
                        paramsString = "$paramsString$name:$value\n"
                        bodyBuilder.addEncoded(name, value)
                    }
                    for ((key , value) in pubParamsMap){
                        paramsString = "$paramsString$key:$value"
                        bodyBuilder.addEncoded(key , value)
                    }
                    oldBody = bodyBuilder.build()
                }

                else -> {
                    for ((key , value) in pubParamsMap){
                        paramsString = "$paramsString$key:$value"
                        bodyBuilder.addEncoded(key , value)
                    }
                    oldBody = bodyBuilder.build()
                }

            }
            oRequest = oRequest.newBuilder().post(oldBody)
                .addHeader("Accept", "application/json")
                .addHeader("Accept-Language", "zh")
                .build()
        }else if ("GET" == oRequest.method) {
            //get方法请求
            //配置文件接口 , api接口 添加默认参数
            val builder = oRequest.url.newBuilder()
            for ((key , value) in pubParamsMap){
                builder.addQueryParameter(key , value)
            }
            val modifiedUrL = builder.build()
            oRequest = oRequest.newBuilder()
                .addHeader("Accept", "application/json")
                .addHeader("Accept-Language", "zh")
                .url(modifiedUrL).build()
        }
        Log.i("----请求头url: ", oRequest.url.toString())
        val decode = URLDecoder.decode(paramsString  , "UTF-8")
        Log.i("----请求头参数: ", decode)
        return chain.proceed(oRequest)
    }


}